package com.xmy.cultivate.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xmy.cultivate.entity.CourseType;
import com.xmy.cultivate.entity.StoreGoods;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 物次库存 Mapper 接口
 * </p>
 *
 * @author hpiggy
 * @since 2024-07-30
 */
public interface StoreGoodsMapper extends BaseMapper<StoreGoods> {

    @Select({"<script>",
            "select t2.id,t2.`name` from store_goods as t1 INNER JOIN course_type as t2 on t1.class_type_id = t2.id",
            "where 1=1",
            "<when test='schoolId!=null and schoolId!=&apos;&apos; '>",
                "and t1.school_id = ${schoolId}",
            "</when>",
            "<when test='subjectsId!=null and subjectsId!=&apos;&apos; '>",
                "and t1.subjects_id = ${subjectsId}",
            "</when>",
            "<when test='yearPart!=null and yearPart!=&apos;&apos; '>",
                "and t1.year_part = ${yearPart}",
            "</when>",
            "<when test='quarterNum!=null and quarterNum!=&apos;&apos; '>",
                "and t1.quarter_num = ${quarterNum}",
            "</when>",
            "<when test='yearClassId!=null and yearClassId!=&apos;&apos; '>",
                "and t1.year_class_id = ${yearClassId}",
            "</when>",
            "<when test='classTypeId!=null and classTypeId!=&apos;&apos; '>",
                "and t1.class_type_id = ${classTypeId}",
            "</when>",
            "and t1.goods_type_id = ${goodsTypeId}",
            "and t1.deleted =0",
            "group by t1.class_type_id",
            "ORDER BY t2.sort asc",
            "</script>"})
    public IPage<CourseType> getClassTypeList(IPage<?> page,@Param("yearPart") Integer yearPart,@Param("quarterNum") Integer quarterNum,@Param("schoolId") Long schoolId,@Param("subjectsId") Long subjectsId,@Param("yearClassId") Integer yearClassId, @Param("classTypeId") Long classTypeId,@Param("goodsTypeId") Long goodsTypeId);

    @Select({"<script>",
            "select IFNULL( MAX(t1.lesson_num),0) as lesson_num from store_goods as t1 INNER JOIN course_type as t2 on t1.class_type_id = t2.id",
            "where 1=1",
            "<when test='schoolId!=null and schoolId!=&apos;&apos; '>",
                "and t1.school_id = ${schoolId}",
            "</when>",
            "<when test='subjectsId!=null and subjectsId!=&apos;&apos; '>",
                "and t1.subjects_id = ${subjectsId}",
            "</when>",
            "<when test='yearPart!=null and yearPart!=&apos;&apos; '>",
                "and t1.year_part = ${yearPart}",
            "</when>",
            "<when test='quarterNum!=null and quarterNum!=&apos;&apos; '>",
                "and t1.quarter_num = ${quarterNum}",
            "</when>",
            "<when test='yearClassId!=null and yearClassId!=&apos;&apos; '>",
                "and t1.year_class_id = ${yearClassId}",
            "</when>",
            "<when test='classTypeId!=null and classTypeId!=&apos;&apos; '>",
                "and t1.class_type_id = ${classTypeId}",
            "</when>",
            "and t1.goods_type_id = ${goodsTypeId}",
            "and t1.deleted =0 ORDER BY t2.sort asc",
            "</script>"})
    public Integer getMaxLessonNum(@Param("yearPart") Integer yearPart,@Param("quarterNum") Integer quarterNum,@Param("schoolId") Long schoolId,@Param("subjectsId") Long subjectsId,@Param("yearClassId") Integer yearClassId, @Param("classTypeId") Long classTypeId,@Param("goodsTypeId") Long goodsTypeId);


    @Select({"<script>",
            "select t1.*,t2.`name` as class_type_name from store_goods as t1 INNER JOIN course_type as t2 on t1.class_type_id = t2.id",
            "where 1=1",
            "<when test='schoolId!=null and schoolId!=&apos;&apos; '>",
                "and t1.school_id = ${schoolId}",
            "</when>",
            "<when test='subjectsId!=null and subjectsId!=&apos;&apos; '>",
                "and t1.subjects_id = ${subjectsId}",
            "</when>",
            "<when test='yearPart!=null and yearPart!=&apos;&apos; '>",
                "and t1.year_part = ${yearPart}",
            "</when>",
            "<when test='quarterNum!=null and quarterNum!=&apos;&apos; '>",
                "and t1.quarter_num = ${quarterNum}",
            "</when>",
            "<when test='yearClassId!=null'>",
                "and t1.year_class_id = ${yearClassId}",
            "</when>",
            "<when test='classTypeId!=null and classTypeId!=&apos;&apos; '>",
                "and t1.class_type_id = ${classTypeId}",
            "</when>",
            "<when test='lessonType!=null and lessonType!=&apos;&apos; '>",
                "and t1.lesson_type = ${lessonType}",
            "</when>",
            "and t1.goods_type_id = ${goodsTypeId}",
            "and t1.deleted =0 ORDER BY t1.id desc",
            "</script>"})
    public List<StoreGoods> getStoreGoodsList(@Param("yearPart") Integer yearPart, @Param("quarterNum") Integer quarterNum,@Param("schoolId") Long schoolId,@Param("subjectsId") Long subjectsId, @Param("yearClassId") Integer yearClassId, @Param("classTypeId") Long classTypeId,@Param("goodsTypeId") Long goodsTypeId,@Param("lessonType") Integer lessonType);

}
